package com.ruoyi.web.controller.system;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.DotificationPictures;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.IDotificationPicturesService;
import com.ruoyi.system.service.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 公告 信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dotificationPictures")
public class DotificationPicturesController extends BaseController
{
    @Autowired
    private IDotificationPicturesService dotificationPicturesService;

    /**
     * 根据通知公告编号获取详细信息
     */
    @GetMapping
    @Anonymous
    public AjaxResult getInfo()
    {
        DotificationPictures  dotificationPictures= dotificationPicturesService.select();
        if(dotificationPictures!=null && !StringUtils.isEmpty(dotificationPictures.getImg())){

            dotificationPictures.setImgs(JSON.parseArray(dotificationPictures.getImg(), String.class));
        }
        return success(dotificationPictures);
    }

    /**
     * 新增通知公告
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody DotificationPictures notice)
    {
        notice.setImg(JSONObject.toJSONString(notice.getImgs()));
        return toAjax(dotificationPicturesService.insertNotice(notice));
    }

    /**
     * 修改通知公告
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody DotificationPictures notice)
    {
        notice.setImg(JSONObject.toJSONString(notice.getImgs()));
        return toAjax(dotificationPicturesService.updateNotice(notice));
    }

    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("files") MultipartFile[]  files)  throws Exception{
        AjaxResult ajax = AjaxResult.success();
        List<String> list =new ArrayList<String>();
        if (files !=null && files.length > 0) {
            for (MultipartFile file : files) {
                String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
                list.add(avatar);

            }
            ajax.put("urls", list);
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }

}
